﻿using System;
using System.Web;

using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.UserManagement;
using BEGDC.CC.BL.Common.Query;
using BEGDC.Utility.Castle;
using BEGDC.CC.Entity;

namespace BEGDC.CC.Web.UserControls
{
	public interface IPageAccess
	{
		AccessControl Access{get; set;}
	}

	/// <summary>
	/// Summary description for AccessControl.
	/// </summary>
	public class AccessControl
	{
		private const string APPLICATION = "App";
		private const string ATTACHMENT = "Att";
		private const string FEEDBACK = "Fee";
		private const string MEETINGMINUTES = "Mee";
		private const string WORKFLOW = "Wor";
		protected int _appId = -1;
		protected string _state = string.Empty;
		protected string _flowType = string.Empty;
		protected string _currentStep = string.Empty;
		protected bool _isQuery = false;
		protected bool _isReadOnly = true;
		protected bool _isPending = false;
		protected bool _cancel = false;
		protected string _tab = string.Empty;
		protected bool _isMgrOrStaff = false;

		// UserControl button

		// ExcutionView button
		protected bool _isShowSuspendButton = false;
		protected bool _isShowResumeButton = false;
		protected bool _isShowDrawbackButton = false;
		protected bool _isShowCallbackButton = false;
		protected bool _isShowCancelButton = false;
		protected bool _isShowReAssignButton = false;
		protected bool _isShowViewCopyButton = false;
		protected bool _isShowModifyButton = false;
		protected bool _isShowPrintButton = false;
		protected bool _isShowFeedbackPrintButton = false;

		#region public property
		public int AppId
		{
			set
			{
				this._appId = value;
				IQuery queryService = AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
				FlowInstance flowInstance = queryService.QueryFlowInstance( _appId );
				if( flowInstance != null )
				{
					_currentStep = flowInstance.CurrentActiveStep.StepCode;
					_flowType = flowInstance.Application.ApplicationType;
					_isPending = flowInstance.State.StateCode == SysDicItem.ProjectStatusFlag.Pending;
					_cancel = flowInstance.State.StateCode == SysDicItem.ProjectStatusFlag.CANCEL;
					if((flowInstance.Application.ApplicationType == WorkflowTypes.CR && flowInstance.Application.IsTransition)
						|| flowInstance.Application.ApplicationType == WorkflowTypes.CV
						|| flowInstance.Application.ApplicationType == WorkflowTypes.OCV
						|| flowInstance.Application.ApplicationType == WorkflowTypes.OCR
						|| flowInstance.Application.ApplicationType == WorkflowTypes.EBDCV
						|| flowInstance.Application.ApplicationType == WorkflowTypes.EBD
						|| flowInstance.Application.ApplicationType == WorkflowTypes.ESCCV
						|| flowInstance.Application.ApplicationType == WorkflowTypes.ESC)
						_isShowPrintButton = true;
					if(flowInstance.Application.ApplicationType == WorkflowTypes.CV)
						_isShowFeedbackPrintButton = true;
				}
				else
					_currentStep = WorkTransition.Draft;
			}
			get
			{
				return this._appId;
			}
		}
		public virtual bool IsShowPrintButton
		{
			get
			{
				return _isShowPrintButton;
			}
		}
		public virtual bool IsShowFeedbackPrintButton
		{
			get
			{
				return _isShowFeedbackPrintButton;
			}
		}
		public virtual string State
		{
			set
			{
				this._state = value;
				//<--Joan 2008/8/22
				switch (_state)
				{
					case SysDicItem.ProjectStatusFlag.View:
					case SysDicItem.ProjectStatusFlag.CANCEL:
					case SysDicItem.ProjectStatusFlag.Pending:
					case SysDicItem.ProjectStatusFlag.Participation:
					case SysDicItem.ProjectStatusFlag.Archive:
						this._isReadOnly=true;
						break;
					default:
						this._isReadOnly=false;
						break;


				}
				//-->end

				/*
					this._isReadOnly = _state == SysDicItem.ProjectStatusFlag.View || _state == SysDicItem.ProjectStatusFlag.CANCEL
					|| _state == SysDicItem.ProjectStatusFlag.Pending || _state == SysDicItem.ProjectStatusFlag.End
					|| _state == SysDicItem.ProjectStatusFlag.Participation || _state == SysDicItem.ProjectStatusFlag.Archive;
				*/
				
				this._isQuery = _state == SysDicItem.ProjectStatusFlag.QUERY;
			}
		}

		public string Tab
		{
			set
			{
				this._tab = value;
			}
		}

		public bool IsShowSuspendButton
		{
			get
			{
				return _isShowSuspendButton && _state == SysDicItem.ProjectStatusFlag.Participation && _tab == APPLICATION && !_isPending;
			}
		}
		public bool IsShowResumeButton
		{
			get
			{
				return _isShowResumeButton && _state == SysDicItem.ProjectStatusFlag.Participation && _tab == APPLICATION;
			}
		}
		public bool IsShowDrawbackButton
		{
			get
			{
				return _isShowDrawbackButton && _state == SysDicItem.ProjectStatusFlag.Participation && _tab == APPLICATION && !_isPending;
			}
		}
		public bool IsShowCallbackButton
		{
			get
			{
                return _isShowCallbackButton && (_state == SysDicItem.ProjectStatusFlag.Participation ||_state == SysDicItem.ProjectStatusFlag.View) && _tab == APPLICATION && !_isPending;
			}
		}
		public bool IsShowCancelButton
		{
			get
			{
                return _isShowCancelButton && (_state == SysDicItem.ProjectStatusFlag.Participation || _state == SysDicItem.ProjectStatusFlag.View) && _tab == APPLICATION && !_isPending;
			}
		}
		public bool IsShowReAssignButton
		{
			get
			{
				return _isShowReAssignButton && _state == SysDicItem.ProjectStatusFlag.Participation && _tab == APPLICATION && !_isPending;
			}
		}
		public bool IsShowModifyButton
		{
			get
			{
				return _isShowModifyButton && _state == SysDicItem.ProjectStatusFlag.Participation && _tab == APPLICATION && !_isPending;
			}
		}
		public bool IsShowViewCopyButton
		{
			get
			{
				return _isShowViewCopyButton;
			}
		}

		public virtual bool IsQuery
		{
			get{ return this._isQuery; }
		}

		public virtual bool IsReadOnly
		{
			get{ return this._isReadOnly; }
		}

		public virtual bool IsAttachmentReadOnly
		{
			get{ return this._isReadOnly; }
		}

		public virtual bool IsButtonVisible(string name)
		{
			return false;
		}

		protected bool ShowSaveButton()
		{
			return true;
		}

		public bool ShowCopyButton()
		{
			return this._appId > 0;
		}

		public bool ShowReplyButton()
		{
			return this._isQuery;
		}

		public bool IsHideAllButton
		{
			get{ return _state == SysDicItem.ProjectStatusFlag.Participation || _state == SysDicItem.ProjectStatusFlag.View
					 || _state == SysDicItem.ProjectStatusFlag.End || _state == SysDicItem.ProjectStatusFlag.Closed;}
		}
		#endregion


		public void CheckExcutionViewButton()
		{
			IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
			_isMgrOrStaff = userService.IsCODManager(((CCIdentity)HttpContext.Current.User.Identity).UserInfo.Id, this._flowType)
					|| userService.IsCODStaff(((CCIdentity)HttpContext.Current.User.Identity).UserInfo.Id, this._flowType);
			if(((CCPrincipal)HttpContext.Current.User).Roles.IndexOf("Applicant") >= 0)
			{
				this._isShowViewCopyButton = true;
			}
			else
			{
				this._isShowViewCopyButton = false;
			}

			if(this._currentStep == WorkTransition.End || this._state == SysDicItem.ProjectStatusFlag.End 
				|| _cancel || this._state == SysDicItem.ProjectStatusFlag.Closed )
			{
				// disable all button
			}
			else if(_isPending)
			{
				if(_isMgrOrStaff)
				{
//					this._isShowReAssignButton = true;
					this._isShowResumeButton = true;
				}
				else
				{
//					this._isShowReAssignButton = false;
//					if(userService.IsCODStaff(((CCIdentity)HttpContext.Current.User.Identity).UserInfo.Id, this._flowType))
//						this._isShowResumeButton = true;
//					else
						this._isShowResumeButton = false;
				}
			}
			else
			{
				if(_isMgrOrStaff)
				{
					this._isShowSuspendButton = true;
					this._isShowCancelButton = true;
					if(_currentStep == WorkTransition.CV_Feedback || _currentStep == WorkTransition.CV_Close
						|| _currentStep == WorkTransition.EV_MeetingMinutes || _currentStep == WorkTransition.EV_PSOApproveMM
						|| _currentStep == WorkTransition.EV_CATApproveMM || _currentStep == WorkTransition.EV_CODStaffCheckMM
						|| _currentStep == WorkTransition.EV_Close || _currentStep == WorkTransition.CR_CRSurvey 
						|| _currentStep == WorkTransition.CR_CloseApp || _currentStep == WorkTransition.CO_CloseApp
						|| _currentStep == WorkTransition.OCV_Close || _currentStep == WorkTransition.OCR_OCRSurvey 
						|| _currentStep == WorkTransition.OCR_CloseApp|| _currentStep == WorkTransition.EBDCV_Close 
						|| _currentStep == WorkTransition.EBD_EBDSurvey || _currentStep == WorkTransition.EBD_CloseApp
						|| _currentStep == WorkTransition.ESCCV_Close || _currentStep == WorkTransition.ESC_ESCSurvey 
						|| _currentStep == WorkTransition.ESC_CloseApp)
						this._isShowModifyButton = true;
				}
				else
				{
					this._isShowSuspendButton = false;
					this._isShowCancelButton = false;
				}

				if(userService.IsLastOperator(((CCIdentity)HttpContext.Current.User.Identity).UserInfo.NameEN, this._appId))
					this._isShowDrawbackButton = true;
				else
					this._isShowDrawbackButton = false;

				if(userService.IsApplicant(((CCIdentity)HttpContext.Current.User.Identity).UserInfo.Id, this._appId))
					this._isShowCallbackButton = true;
				else
					this._isShowCallbackButton = false;

				if((userService.IsCODManager(((CCIdentity)HttpContext.Current.User.Identity).UserInfo.Id, this._flowType)) && 
					(_currentStep != WorkTransition.EV_Request && _currentStep != WorkTransition.EV_Assign
					&& _currentStep != WorkTransition.CV_Apply && _currentStep != WorkTransition.CV_Assignment 
					&& _currentStep != WorkTransition.CR_Request && _currentStep != WorkTransition.CR_AssignApp 
					&& _currentStep != WorkTransition.CO_Request && _currentStep != WorkTransition.CR_AssignApp))
					this._isShowReAssignButton = true;
				else
					this._isShowReAssignButton = false;
			}
		}
	}
}
